package org.cupk2.service.controller;

import org.cupk2.base.domain.CupkUserContextLoader;
import org.cupk2.files.service.ResourceFileServiceInter;
import org.cupk2.zhzm.dto.web.WebTagDto;
import org.cupk2.zhzm.domain.ZhzmTag;
import org.cupk2.zhzm.service.ZhzmKnowServiceInter;
import org.cupk2.zhzm.service.ZhzmKnowTagServiceInter;
import org.cupk2.zhzm.service.ZhzmTagServiceInter;
import org.cupk2.tools.bean.CupkBeanUtils;
import org.cupk2.tools.web.CupkResponseCode;
import org.cupk2.tools.web.CupkResponseResult;
import org.cupk2.tools.web.dto.SearchKeyDto;
import org.cupk2.wdap.service.WdapExtendFileServiceInter;
import org.cupk2.wdap.service.WdapTaskServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 知识
 *
 * @author cbtg自动生成  2025-2-4 12:13:51
 */
@RestController
@RequestMapping("/api/wtag")
public class WebTagController {
    @Autowired
    private ZhzmKnowServiceInter zhzmKnowServiceImpl;
    @Autowired
    private WdapExtendFileServiceInter wdapExtendFileServiceImpl;
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    @Autowired
    private WdapTaskServiceInter wdapTaskServiceImpl;
    @Autowired
    private ZhzmKnowTagServiceInter zhzmKnowTagServiceImpl;
    @Autowired
    private ZhzmTagServiceInter zhzmTagServiceImpl;

    /**
     * 查询系统标签
     *
     * @param key
     * @return
     */
    @PreAuthorize("@cupkAction.has('wtag.search')")
    @PostMapping("/search")
    public CupkResponseResult queryAll(@RequestBody SearchKeyDto key) {
        List<ZhzmTag> tags = zhzmTagServiceImpl.getTags(key.getKey());
        List<WebTagDto> bTags = new ArrayList<>();
        for (ZhzmTag tag : tags) {
            WebTagDto td = new WebTagDto();
            CupkBeanUtils.copyProperties(tag, td);
            td.setType("2");
            bTags.add(td);
        }
        if (tags.size() <= 0) {
            WebTagDto noneTag = new WebTagDto();
            noneTag.setType("3");
            noneTag.setTagkey(key.getKey());
            noneTag.setName(key.getKey());
            bTags.add(noneTag);
        }
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, bTags);
    }


    /**
     * 获得我的标签
     *
     * @return
     */
    @PreAuthorize("@cupkAction.has('wtag.my')")
    @PostMapping("/my")
    public CupkResponseResult my() {

        List<WebTagDto> tags = zhzmTagServiceImpl.getUserTags();

        if (CupkUserContextLoader.isLogin()) {
            tags.add(new WebTagDto("0", "我的知识", "SYS:MY_KNOW", zhzmKnowServiceImpl.getKnowNumByUser(CupkUserContextLoader.getCurrentUserKey())));
        }
        tags.add(new WebTagDto("0", "最新知识", "SYS:NEW_KNOW", zhzmKnowServiceImpl.getNewKnowsNum(CupkUserContextLoader.getCurrentUserKey())));
        tags.sort(new Comparator<WebTagDto>() {
            @Override
            public int compare(WebTagDto o1, WebTagDto o2) {
                return o1.getType().compareTo(o2.getType());
            }
        });
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, tags);
    }
}
